package service

import (
	"context"
	"jk-time/webook/account/domain"
	"jk-time/webook/account/repository"
	"jk-time/webook/pkg/logger"
)

type AccountService interface {
	AddCredit(ctx context.Context, creadit domain.Creadit) error
}
type accountService struct {
	repo repository.AccountRepository
	l    logger.Logger
}

func NewAccountService(repo repository.AccountRepository, l logger.Logger) AccountService {
	return &accountService{repo: repo, l: l}
}

func (a *accountService) AddCredit(ctx context.Context, creadit domain.Creadit) error {
	ok := a.repo.Verify(ctx, creadit.Biz, creadit.BizId)
	if ok {
		return nil
	}
	err := a.repo.AddCredit(ctx, creadit)
	go func() {
		if err := a.repo.SetCacheAccount(ctx, creadit.Biz, creadit.BizId); err != nil {
			a.l.Error("缓存打赏记账记录失败,", logger.String("biz", creadit.Biz),
				logger.Int64("biz_id", creadit.BizId), logger.Error(err))
		}
	}()
	return err
}
